﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;
namespace MyWeb.Control.Home
{
    public partial class Menu : System.Web.UI.UserControl
    {
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Data.Page> list = PageService.Page_GetByAll();
            if (list.Count > 0)
            {
                this.ltr_menu.Text = "<ul id=\"menu_mid\">";
                for (int i = 1; i < list.Count; i++)
                {

                    //có con
                    if (list[i - 1].Level.Length < list[i].Level.Length)
                    {
                        this.ltr_menu.Text += "<li><a href=\"" + list[i - 1].Link + "\" target=\"_self\">" + list[i - 1].Name + "</a><ul style=\"visibility: hidden;\"> \n";
                    }

                    else if (list[i - 1].Level.Length == list[i].Level.Length)
                    {
                        this.ltr_menu.Text += "<li><a href=\"" + list[i - 1].Link + "\" target=\"_self\">" + list[i - 1].Name + "</a></li>\n";
                    }
                    //không có con
                    else if (list[i - 1].Level.Length > list[i].Level.Length)
                    {
                        this.ltr_menu.Text += "<li><a href=\"" + list[i - 1].Link + "\" target=\"_self\">" + list[i - 1].Name + "</a></li>\n";
                        int n = (list[i - 1].Level.Length - list[i].Level.Length) / 5;
                        this.ltr_menu.Text += Inma(n);
                    }
                }

                if (list[list.Count - 2].Level.Length < list[list.Count - 1].Level.Length)
                {

                    this.ltr_menu.Text += "<li><a href=\"" + list[list.Count - 1].Link + "\" target=\"_self\">" + list[list.Count - 1].Name + "</a></li>\n";
                    int s = (list[list.Count - 1].Level.Length - 5) / 5;
                    this.ltr_menu.Text += Inma(s);
                }
                else if (list[list.Count - 2].Level.Length == list[list.Count - 1].Level.Length)
                {

                    this.ltr_menu.Text += "<li><a href=\"" + list[list.Count - 1].Link + "\" target=\"_self\">" + list[list.Count - 1].Name + "</a></li>\n";
                    int s = (list[list.Count - 1].Level.Length - 5) / 5;
                    this.ltr_menu.Text += Inma(s);
                }
                else if (list[list.Count - 2].Level.Length > list[list.Count - 1].Level.Length)
                {
                    this.ltr_menu.Text += "<li><a href=\"" + list[list.Count - 1].Link + "\" target=\"_self\">" + list[list.Count - 1].Name + "</a></li>\n";
                }
                this.ltr_menu.Text += "</ul>";
            }
            }
        }

        string Inma(int a)
        {
            string str = "";
            for (int j = 1; j <= a; j++)
            {
                str += "</ul></li>";
            }
            return str;
        }
    }
}